import Vue from 'vue'
import Vuex from 'vuex'
import createPersistedState from 'vuex-persistedstate'
Vue.use(Vuex)

export default new Vuex.Store({
  plugins: [createPersistedState({
    storage: window.sessionStorage
  })],
  state: {
    //token信息
    token: '',
    //登录用户信息
    user: {
      userId: 0,
      username: '',
      nickname: '',
      position: false,
      avatar: ''
    },
    //重置
    resetUser: {
      userId: 0,
      username: '',
      nickname: '',
      position: false,
      avatar: ''
    },
    //文章详细id
    articleDetailId: 0,
    index: '1',
    //保存当前编辑的内容，防止刷新丢失
    contentMd: '',
    title: '',
    draftId: 0,
    articleId: 0,
    //标签的颜色
    color: [
      "orange",
      "gold",
      "yellow",
      "lime",
      "green",
      "cyan",
      "blue"
    ],
    //以下的配置是为了保留浏览的状态
    //设置首页的分页信息
    pageNo: 1,
    pageSize: 5,
    currentId: {
      //当前点击的分类Id 0表示未点击
      currentCategoryId: 0,
      //当前点击的标签Id 0表示未点击
      currentTagsId: 0
    }
  },
  mutations: {
    changeIndex(state, name) {
      state.index = name;
    },
    setContentMd(state, contentMd) {
      state.contentMd = contentMd;
    },
    setTitle(state, title) {
      state.title = title
    },
    setDraftId(state, draftId) {
      state.draftId = draftId
    },
    setArticleId(state, articleId) {
      state.articleId = articleId
    },
    setIndexPageInfo(state, currentId) {
      state.pageNo = 1;
      state.currentId = currentId;
    },
    setPageNo(state, pageNo) {
      state.pageNo = pageNo;
    },
    setArticleDetailId(state, articleDetailId) {
      state.articleDetailId = articleDetailId;
    },
    serUser(state, user) {
      state.user = user;
    },
    setToken(state, token) {
      state.token = token;
    },
    setAvatar(state, avatar) {
      state.user.avatar = avatar;
    },
    setNickname(state, nickname) {
      state.user.nickname = nickname;
    },
    resetUser(state) {
      state.user = state.resetUser;
      state.token = '';
      //token存到本地
      window.localStorage.setItem("token", state.token);
      //将用户信息放到本地
      window.localStorage.setItem("user", JSON.stringify(state.user));
    }
  },
  actions: {
  },
  modules: {
  }
})
